<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */

class Action extends admin_abs
{
   function index()
   {
       $mid = _GET('mid', 0);
       if ($mid) {
           $data = _model(_t('resource'))->getList(array('model_id'=>$mid), 'ORDER BY add_time DESC ', new Pager(20));
           $attribute = _model(_t('attribute'))->getList(array('model_id'=>$mid));
           foreach ($attribute as $key => $value) {
                if ($value['type'] =='text' || $value['type'] =='file') {
                    unset($attribute[$key]);
                }
           }
           Smarty3::instance()->assign('attribute', $attribute);
           Smarty3::instance()->assign($data);
           Smarty3::instance()->assign('mid', $mid);
           Smarty3::instance()->display('resource_list.html');
       } else {
           $model_list = _model(_t('model'))->getList();
           Smarty3::instance()->assign('data', $model_list);
           Smarty3::instance()->display('resource_pickmodel.html');
       }
   }

   function create()
   {
       if ($_SERVER['REQUEST_METHOD'] === 'POST') {
          $attribute = _POST('attribute', array());
          $add_time  = time();
          $title     = _POST('title', '');
          $mid       = _POST('mid', 0);
          $t_attribute = _model(_t('attribute'))->getList(array('model_id'=>$mid), ' ORDER BY view_order ASC ');
          if (!$title) {
            msg('标题不能为空');
          }
          $res_id = _model(_t('resource'))->create(array('title'=>$title, 'model_id'=>$mid, 'add_time'=>$add_time));
          if (!$res_id) {
            msg('添加失败');
          }
          foreach ($t_attribute as $key => $value) {
            if ($value['type'] == 'file') {
                if (isset($_FILES["attribute_".$value['id']]) && is_uploaded_file($_FILES["attribute_".$value['id']]["tmp_name"])) {
                    $attribute[$value['id']] = Storage::add($_FILES["attribute_".$value['id']]["tmp_name"], $_FILES["attribute_".$value['id']]["name"]);
                }
            }
          }
          foreach ($attribute as $key => $value) {
              if (!empty($value)) {
                  _model(_t('attribute_value'))->create(array('res_id'=>$res_id, 'attribute_id'=>$key, 'value'=>$value));
              }
          }
          msg('添加成功','?url=resource/create&mid='.$mid);
       } else {
            $mid = _GET('mid', 0);
            if ($mid && $model_info = _model(_t('model'))->read(array('id'=>$mid))) {
                $attribute = _model(_t('attribute'))->getList(array('model_id'=>$mid), ' ORDER BY view_order ASC ');
                foreach ($attribute as $key => $value) {
                     if ($value['type'] == 'select') {
                         $attribute[$key]['value'] = array_map('trim', explode("\n", $value['value']));
                     }
                }
                Smarty3::instance()->assign('mid', $mid);
                Smarty3::instance()->assign('attribute', $attribute);
                Smarty3::instance()->display('resource_create.html');
            }
       }
   }

   function update()
   {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $res_id =  _POST('rid', 0);
            $attribute = _POST('attribute', array());
            $add_time  = time();
            $title     = _POST('title', '');
            $mid       = _POST('mid', 0);
            $t_attribute = _model(_t('attribute'))->getList(array('model_id'=>$mid), ' ORDER BY view_order ASC ');
            if (!$title) {
            msg('标题不能为空');
            }
            _model(_t('resource'))->update(array('id'=>$res_id), array('title'=>$title));

            foreach ($t_attribute as $key => $value) {
                if ($value['type'] == 'file') {
                    if (isset($_FILES["attribute_".$value['id']]) && is_uploaded_file($_FILES["attribute_".$value['id']]["tmp_name"])) {
                        $attribute[$value['id']] = Storage::add($_FILES["attribute_".$value['id']]["tmp_name"], $_FILES["attribute_".$value['id']]["name"]);
                    }
                }
            }
            foreach ($attribute as $key => $value) {
                 if (!$r = _model(_t('attribute_value'))->read(array('res_id'=>$res_id, 'attribute_id'=>$key))) {
                     _model(_t('attribute_value'))->create(array('res_id'=>$res_id, 'attribute_id'=>$key, 'value'=>$value));
                 } else {
                     _model(_t('attribute_value'))->update(array('res_id'=>$res_id, 'attribute_id'=>$key), array('value'=>$value));
                 }
            }
            msg('更新成功','?url=resource&mid='.$mid);
        } else {
           $res_id = _GET('id', 0);
           if ($res_id && $res_info = _model(_t('resource'))->read(array('id'=>$res_id))) {
               $attribute = _model(_t('attribute'))->getList(array('model_id'=>$res_info['model_id']), ' ORDER BY view_order ASC ');
               foreach ($attribute as $key => $value) {
                    if ($value['type'] == 'select') {
                       $attribute[$key]['value'] = array_map('trim', explode("\n", $value['value']));
                    }
               }
               Smarty3::instance()->assign('res_info', $res_info);
               Smarty3::instance()->assign('attribute', $attribute);
               Smarty3::instance()->assign('mid', $res_info['model_id']);
               Smarty3::instance()->display('resource_update.html');
           } else {
                msg('参数有误或不存在这个内容');
           }
        }
   }

   function delete()
   {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $id_array = array_filter(_POST('check', array()));
            $rid = end($id_array);
            $res_info = _model(_t('resource'))->read(array('id'=>$rid));
            foreach ($id_array as $key => $value) {
                _model(_t('resource'))->delete(array('id'=>$value));
                _model(_t('attribute_value'))->delete(array('res_id'=>$value));
            }
            redirect('?url=resource&mid='.$res_info['model_id']);
        } else {
            $id = _GET('id', 0);
            if ($id && $res_info = _model(_t('resource'))->read(array('id'=>$id))) {
                _model(_t('resource'))->delete(array('id'=>$id));
                _model(_t('attribute_value'))->delete(array('res_id'=>$id));
                redirect('?url=resource&mid='.$res_info['model_id']);
            }
        }
   }
   
   function del_att_value()
   {
       $res_id = _GET('res_id', 0);
       $attribute_id = _GET('attribute_id', 0);
       _model(_t('attribute_value'))->update(array('res_id'=>$res_id, 'attribute_id'=>$attribute_id), array('value'=>''));
       echo '删除成功';
   }
   
}
?>